package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.qf.mapper.DtsGoodsAttributeMapper;
import com.qf.pojo.DtsGoodsAttribute;
import com.qf.service.GoodsAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodsAttributeServiceImpl implements GoodsAttributeService {
    @Autowired
    private DtsGoodsAttributeMapper dtsGoodsAttributeMapper;


    @Override
    public List<DtsGoodsAttribute> findListByGoodsId(Integer id) {
        return dtsGoodsAttributeMapper.selectList(new QueryWrapper<DtsGoodsAttribute>()
                .eq("goods_id",id)
                .eq("deleted",false));
    }

    @Override
    public void update(DtsGoodsAttribute[] attributes, Integer goodsId) {
        dtsGoodsAttributeMapper.updateByGoodsId(goodsId);
        if (attributes==null||attributes.length==0){
            return;
        }
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setUpdateTime(new Date());
            attribute.setDeleted(false);
            if (attribute.getId()!=null){
                dtsGoodsAttributeMapper.updateById(attribute);
            }else{
                attribute.setAddTime(new Date());
                attribute.setGoodsId(goodsId);
                dtsGoodsAttributeMapper.insert(attribute);
            }
        }

    }

    @Override
    public void create(DtsGoodsAttribute[] attributes,Integer goodsId) {
        if (attributes==null||attributes.length==0){
            return;
        }
        for (DtsGoodsAttribute attribute : attributes) {
            attribute.setGoodsId(goodsId);
            attribute.setAddTime(new Date());
            attribute.setUpdateTime(new Date());
            attribute.setDeleted(false);
            dtsGoodsAttributeMapper.insert(attribute);
        }


    }


}
